<?php
    require_once '../model/Classe.php';

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and opejjhhhhhhhhhn the template in the editor.
 */

class ClasseManager {
    private $bdd;
    private $listeClasse;
    
    function __construct($bdd) {
        $this->bdd = $bdd;
        $this->setListeClasse();
    }
    
    public function add(Classe $classe) {

        $q = $this->bdd->prepare('INSERT INTO CLASSES (libelle) VALUES(:libelle)');
        $q->bindValue(':libelle', $classe->getLibelleClasse());
        $q->execute();
    }

    public function delete(Classe $classe) {
        $id = $classe->getIdClasse();
        $this->connexion();
        $bdd->exec('DELETE FROM CLASSES WHERE idClasse = ' . $id);
    }
    
    public function findClasse(Classe $classe){             
        $q = $this->bdd->prepare('SELECT * FROM CLASSES WHERE idClasse = '. $classe->getIdClasse());
        $q->execute();
        $donnees = $q->fetchAll();
        return $donnees;
    }
    
    public function findIdClasseByLibelle($libelle){
        $donnees = array();
        $q = $this->bdd->prepare('SELECT * FROM CLASSES WHERE libelle = '. $libelle);
        $q->execute();
        //$donnees = $q->fetchAll();
        while ($data = $q->fetch()){
            //array_push($this->listeClasse, $data['libelle']);
            $donnees=$data['id'];
        }
        return $donnees;
    }
    
    public function getListeClasse() {

        return $this->listeClasse;
    }
    
    public function setListeClasse(){
        $this->listeClasse=array();
                
        $q = $this->bdd->prepare('SELECT * FROM CLASSES');
        $q->execute();
        
        while ($data = $q->fetch()){
            //array_push($this->listeClasse, $data['libelle']);
            $this->listeClasse[]=$data['libelle'];
        }
    }
    
     public function getListClasse() {
        $listClasse = array();
        $req = $this->bdd->prepare('SELECT * FROM CLASSES ');
        $req->execute();
        while ($donnees = $req->fetch()) {
            $classe = new Classe($donnees["idClasse"], $donnees["libelle"]);
            array_push($listClasse, $classe);
        }
        return $listClasse;
    }

}
